<?php
/**
 * Object Relational Persistence Mapping Class for table: calendar_relationship
 *
 * @package	com.athena
 * @author	LHV
 */
class CalendarRelationship extends ORDataObject {

	/**#@+
	 * Fields of table: calendar_relationship mapped to class members
	 */
	var $relationship_id		= '';
	var $person_id				= '';
	var $relational_person_id	= '';
	
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'calendar_relationship';

	/**
	 * Primary Key
	 */
	var $_key = 'relationship_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'CalendarRelationship';

	/**
	 * Handle instantiation
	 */
	function CalendarRelationship() {
		parent::ORDataObject();
	}
	
	public function addUser($personId, $relational_personId){
		$sql = array(
				'cols'	=> "count(*) sum", 
				'from' 	=> "$this->_table",
				'where'	=> " person_id = $personId AND relational_person_id = $relational_personId "
				);
		$ds = new Datasource_sql();
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		//print_r($result);
		if($result['0']['sum'] > 0){
			return 1;
		}
		$data = array(
					'person_id' 			=> $personId,
					'relational_person_id' 	=> $relational_personId
				);
		$this->populateArray($data);
		$this->persist();
		return 0; 	
	}
	
	public function removeUser($personId, $relational_personId){
		$sql = " DELETE FROM $this->_table WHERE person_id = $personId 
											AND relational_person_id = $relational_personId";
		$this->_execute($sql);
	}
	
	// TODO ptnguyen 2010-03-17
	// human resource module
	// employee table contain all information of employee, no need to join person
	public function getRelationalPerson($personId){
		$field	=	L10nHelper::changeField(L10nHelper::getLocale(), 'name');
		
		$ds = new Datasource_sql();
		$sql = array(
					'cols'	=> "r.relational_person_id, p.name name, et.$field employee_type ",
					'from'	=> "$this->_table r
								LEFT JOIN person p ON (r.relational_person_id = p.person_id)
								LEFT JOIN employee e ON (r.relational_person_id = e.person_id)
								LEFT JOIN employee_type et ON (e.employee_type_id = et.employee_type_id)
								LEFT JOIN user u ON (u.person_id = r.relational_person_id)",								
					'where'	=> "r.person_id = $personId AND u.disabled = 'no'",
					'ORDER BY' => "name"
				);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	public function checkExist($person_id){
		$sql = array(
				'cols'	=> "count(person_id) sum",
				'from'	=> $this->_table,
				'where'	=> "person_id = $person_id"
				);
		$ds = new Datasource_sql();
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	public function getRelationalPersonName($personId){
		
		$ds = new Datasource_sql();
		$sql = array(
					'cols'	=> "r.relational_person_id person_id , p.name name ",
					'from'	=> "$this->_table r
								LEFT JOIN person p ON (r.relational_person_id = p.person_id)
								LEFT JOIN user u ON (u.person_id = r.relational_person_id)",
					'where'	=> "r.person_id = $personId AND u.disabled = 'no'",
					'ORDER BY' => "name"
				);
		$ds->setup($this->_db, $sql);
		return $ds->toArray('person_id', 'name');
	}
	
	public function getRelationalPersonId($personId){
		$ds = new Datasource_sql();
		$sql = array(
					'cols'	=> "relational_person_id ",
					'from'	=> "$this->_table",
					'where'	=> "person_id = $personId"
				);
		$ds->setup($this->_db, $sql);
		return $ds->toArray('relational_person_id');
	}
}
?>
